<template>
  <div id="home" style="position: relative">
    <HeaderPage />
    <div class="content">
      <div class="courseContent">
        <!-- 顶部课程名称 -->
        <div
          style="
            display: flex;
            justify-content: space-between;
            background-color: #fafafa;
            height: 76px;
            padding: 0 20px;
            border-radius: 10px;
            border: 1px solid #e8e8e8;
          "
        >
          <div style="color: #353535; font-size: 24px; padding: 10px 0">
            {{ course.courseName }}
            <div style="font-size: 12px; color: #888888; margin: 5px 0">
              授课老师：<span style="color: #3399ee">{{
                teacher.nickName
              }}</span>
            </div>
          </div>
          <div
              class="exit"
            @click="exitCourse"
            style="
              background-color: #1e9fff;
              font-size: 12px;
              width: 86px;
              height: 30px;
              color: white;
              margin: 10px 0;
              display: flex;
              justify-content: space-around;
              align-items: center;
            "
          >
            <div>
              <img
                style="width: 12px; height: 14px"
                src="../../assets/images/exit.png"
                alt=""
              />
            </div>
            <div>退出课程</div>
          </div>
        </div>

        <!-- 课程内容列表 -->
        <div style="display: flex; text-align: center; margin: 15px 0">
          <div
            style="
              width: 100px;
              height: 100px;
              margin: 10px 20px;
              color: #666666;
              font-size: 15px;
              display: flex;
              flex-direction: column;
              justify-content: center;
            "
            v-for="(item, index) in list"
            :key="index"
            class="aaa"
            @click="goPage(item.id)"
          >
            <div>
              <img style="width: 28px; height: 28px" :src="item.icon" alt="" />
            </div>
            <div>
              {{ item.title }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import HeaderPage from "@/components/HeaderPage.vue";
import { getCourseInfo, exit } from "@/api/course.js";
export default {
  data() {
    return {
      courseId: "",
      course: {},
      teacher: {},
      list: [
        {
          id: 1,
          icon: require("../../assets/images/student.png"),
          title: "班级学生",
        },
        {
          id: 2,
          icon: require("../../assets/images/work.png"),
          title: "作业",
        },
      ],
    };
  },
  components: {
    HeaderPage,
  },
  mounted() {
    this.courseId = this.$route.query.courseId;
    this.getCourse();
  },
  methods: {
    // 获取课程信息
    getCourse() {
      getCourseInfo(this.courseId)
        .then((res) => {
          this.course = res.course;
          this.teacher = res.teacher;
        })
        .catch(() => {});
    },

    // 跳转到指定页面
    goPage(id) {
      // 跳转到班级学生
      if (id === 1) {
        this.$router.push({
          path: "/classStudent",
          query: {
            courseId: this.courseId,
          },
        });
      }

      // 跳转到作业
      if(id === 2){
        this.$router.push({
          path: "/work",
          query: {
            courseId: this.courseId,
          },
        });
      }
    },

    // 退出课程
    exitCourse() {
      exit(this.courseId)
        .then((res) => {
          if (res.code === 200) {
            this.$message.success("退出课程成功");
            this.$router.push("/")
          }
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.content {
  background-image: url("../../assets/images/home-bg.jpg");
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  height: 900px;
  position: fixed;
  width: 100%;
  left: 0;
  top: 80px;
  overflow: auto;
}

.courseContent {
  padding: 20px;
  width: 80%;
  background-color: white;
  margin: 60px auto;
  border-radius: 10px;
}
.course:hover {
  cursor: pointer;
}
.aaa:hover {
  cursor: pointer;
  background-color: #fafafa;
}
.exit:hover{
  cursor:pointer;
}
</style>
